<?php

namespace app\api\controller\driver;

use app\common\controller\Api;
use app\common\service\driver\DriverLeaveService;
use think\Validate;

/**
 * 司机端-请假申请接口
 * @ApiWeigh (84)
 */
class Leave extends Api
{
    protected $beforeActionList = [
        'create_before' => ['only' => 'create'],
    ];
    protected $noNeedLogin = ['leave_type_list'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiInternal
     */
    public function create_before()
    {
        $params = $this->request->post();
        $validate = new \app\common\validate\driving\Leave();
        if (!$validate->check($params, [], 'create')) {
            $this->error($validate->getError());
        }
    }


    /**
     * 请假类型列表
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"请假类型列表","time":"1721180168","data":[{"id":1,"name":"事假"}]})
     **/
    public function leave_type_list()
    {
        $this->success('请假类型列表', get_one_two_array(config('site.leave_type_list'), 'id', 'name'));
    }

    /**
     * 请假
     *
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="integer", required=true, description="请假类型")
     * @ApiParams (name="start_time", type="string", required=true, description="开始时间 2024-07-11 00:00")
     * @ApiParams (name="end_time", type="string", required=true, description="结束时间 2024-07-11 10:00")
     * @ApiParams (name="total_time", type="string", required=true, description="时长，单位：小时")
     * @ApiParams (name="desc", type="string", required=true, description="请假事由")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "提交成功","time": "1695260676","data": null})
     **/
    public function create()
    {
        $type = $this->request->post('type');
        $start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
        $total_time = $this->request->post('total_time');
        $desc = $this->request->post('desc');
        $params = compact('type','start_time','end_time','total_time','desc');
        $user = $this->auth->getUser();
        DriverLeaveService::create($params, $user);
        $this->success('提交成功，请耐心等待审核');
    }

    /**
     * 列表
     *
     * @ApiMethod (POST)
     * @ApiParams (name="page", type="integer", required=true, description="页数 不传值默认1")
     * @ApiParams (name="page_size", type="integer", required=true, description="每页数量 不传值默认10")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturnParams (name=data.total,type="integer",description=总数量)
     * @ApiReturnParams (name=data.data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"列表","time":"1721311775","data":{"total":1,"data":[{"id":1,"driver_id":1,"type":"请假类型","start_time":"开始时间","end_time":"结束时间","total_time":"时长，单位：小时","desc":"请假事由","createtime":"创建时间","audit_status":"审核状态:1=待审核,2=审核通过,3=审核不通过","audit_desc":"驳回理由"}]}})
     **/
    public function list()
    {
        $page = $this->request->post('page');
        $page_size = $this->request->post('page_size');
        $user = $this->auth->getUser();
        $list = DriverLeaveService::list($page, $page_size, $user);
        $this->success('列表', $list);
    }

}
